#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年10月20日

@author: chenyongbing
'''
import sys,os,time,urllib2,urllib,re,json
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
import logging
import inspect

#60.到访次数分布 - GroupList - 
#61.到访次数驻店时长分布 - GroupList - 

#48. 店铺概览：客流质量



#50. 趋势排行.png
#25. 连锁店概览：趋势分布图
#39. 顾客类型：关键指标
#40. 顾客类型：趋势指标
#41.   驻店时长：关键指标
# 43. 驻店时长：深访趋势 & 44. 驻店时长：跳出趋势

sys.path.append('../../')
from base.zhimind_datas import myZhimindDatas
from base.time_method import myTimeMethod



class VerifyZhimindWSData():
    def __init__(self,base='http://192.168.1.203:8080/zm-ws-3.0/api/v3',runType=1,token=10002000):
        self.factory = None
        self.dis_factory = "Apple" 
        self.mac_type = 'global'  #all , global , local 随机mac
        self.macType = 1  # 1 安卓 、 0 苹果 and 随机
        self.rate_digit = 3
        self.freq_digit = 1
        self.dur_digit = 0
        self.people_digit = 0
        self.amount_digit = 0
        self.runType = runType # 0 check,1 zhimind , 2 ws 
        self.base = base
        self.token = token
        
    def getResponse(self,url,detailMap={},data=None,type='post'):
        if 'Token' not in detailMap.keys():
            detailMap['Token'] = self.token
        
        if data==None:
            groupList = []
            listKeys = []
            for key,values in detailMap.iteritems():
                if isinstance(values,list):
                    listKeys.append(key)
                    
                    #del detailMap[key]
            for lk in listKeys:
                values = detailMap[lk]
                del detailMap[lk]
                count = 0
                for value in values:
                    count+=1
                    detailMap[lk+'_'+str(count)] = value
            
            data=urllib.urlencode(detailMap)
            for lk in listKeys:
                data = re.sub('%s_[\d]+'%lk,lk,data)
            
        else:
            data = data
        st = time.time()
        if type=='post':
            logging.info( url+"   "+data)
            req=urllib2.Request(url,data)
#             print req
            response=urllib2.urlopen(req)
        else:
            logging.info( url+'?'+data)
            response = urllib2.urlopen(url+'?'+data)
            
        page=response.read()
        et = time.time()
        logging.info( 'use time:%s'%(et-st))
        #print "page",page
        return json.loads(page)
    def get_value_from_dict_by_key(self,dict1={},key='',sub_key=None):
        if dict1.has_key(key):
            if sub_key!=None:
                return dict1[key][sub_key]
            return dict1[key]
        else:
            return 0
        
    def get_params_from_method(self,argvalues):
        keys = argvalues.args
        keys.remove('self')
        locals = argvalues.locals
        
        datas = {}
        
        for k,v in locals.items():
            if k in keys:
                nk = re.sub('^%s'%k[0],k[0].upper(),k)
                if nk=='TimeType':
                    if v == 'day':v=1
                    if v == 'week':v=2
                    if v == 'month':v=3
                datas[nk] = v
                
        return datas
    
    def data_cross_bubble(self,requestType=4,timeType=1,startTime=None,groupList=[],groupType=4,xType=1,yType=1):
        '''25.    连锁店概览：趋势分布图'''
        #http://10.4.15.122:8080/zm-ws-3.0/api/v3/over_view/data_cross_bubble
        api_name = 'over_view/data_cross_bubble'
        
        startTime,endTime = myTimeMethod.get_st_et_by_timeType(startTime,timeType)
        if self.runType in [0,1]:
        
            if timeType == 1:
                timeType = 'day'
            if timeType == 2:
                timeType = 'week'
            if timeType == 3:
                timeType = 'month'
            
            '''
                1：到达客流  -- all
                2：入店客流  -- all
                3：入店率   -- all
                4：驻店时长   -- all
                5：老顾客   -- 废弃
                6：老顾客占比  -- 安卓
                7：到访频次  -- 废弃
                8：入店人数 -- all
                9：来访周期  -- 安卓
                10：新顾客  -- 废弃
                11：新顾客占比  --安卓
            '''
            

            
            customerFlowDatas = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( distinct=False, groups=groupList, groupType=groupType, st=startTime, et=endTime,factory=None, dis_factory=None)
            walkbyFlowDatas = myZhimindDatas.get_walkby_cnt_from_mac_walkby_stats_by_group(groups=groupList, groupType=groupType, st=startTime, et=endTime, distinct=False, macType=None)
            #customerRateDatas = myZhimindDatas.get_walkby_cnt_from_mac_walkby_stats_by_group(groups=groupList, groupType=groupType, st=startTime, et=endTime, distinct=False,macType=self.macType)
           
            
            avgCustomerDurDatas = myZhimindDatas.get_customer_dur_from_mac_customer_stats_by_group(groups=groupList, groupType=groupType, st=startTime, et=endTime, factory=None, dis_factory=None, atype='avg')
            customerMacCountDatas = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( distinct=True, groups=groupList, groupType=groupType, st=startTime, et=endTime,factory=None, dis_factory=None)
            repeatCustomerMacCountDatas = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( distinct=True,role=10, groups=groupList, groupType=groupType, st=startTime, et=endTime,factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type,macType=self.macType)
            newCustomerMacCountDatas = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( distinct=True,role=3, groups=groupList, groupType=groupType, st=startTime, et=endTime,factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type,macType=self.macType)
            customerVisitCycleDatas = myZhimindDatas.get_avg_visit_cycle_from_customer_type_stats( timeType=timeType, groups=groupList, groupType=groupType, day=startTime)
            
            dataList = []
            for group in groupList:
                customerFlow = self.get_value_from_dict_by_key(customerFlowDatas, int(group))
                walkbyFlow = self.get_value_from_dict_by_key(walkbyFlowDatas, int(group))
                    
                allFlow = customerFlow + walkbyFlow
                
    
                customerRate = self.__calculation_rate__(customerFlow, customerFlow+walkbyFlow)
                
                avgCustomerDur = self.get_value_from_dict_by_key(avgCustomerDurDatas, int(group))
        
                customerMacCount = self.get_value_from_dict_by_key(customerMacCountDatas, int(group))
                
                
                repeatCustomerMacCount  = self.get_value_from_dict_by_key(repeatCustomerMacCountDatas, int(group))
                newCustomerMacCount = self.get_value_from_dict_by_key(newCustomerMacCountDatas, int(group))
                
                customerVisitCycle = self.get_value_from_dict_by_key(customerVisitCycleDatas, int(group))
                
                
                if xType == 1:
                    xdata = allFlow
                if yType == 1:
                    ydata = allFlow
                if xType ==2:
                    xdata = customerFlow
                if yType == 2:
                    ydata = customerFlow
                if xType == 3:
                    xdata =  customerRate
                if yType == 3:
                    ydata = customerRate
                   
                if xType == 4:
                    xdata = round(avgCustomerDur/1000.0,0)
                if yType == 4:
                    ydata = round(avgCustomerDur/1000.0,0)
                    
                if xType == 6:
                    xdata = self.__calculation_rate__(repeatCustomerMacCount,(repeatCustomerMacCount+newCustomerMacCount))
                if yType == 6:
                    ydata = self.__calculation_rate__(repeatCustomerMacCount,(repeatCustomerMacCount+newCustomerMacCount))
                    
                if xType == 8:
                    xdata = customerMacCount
                if yType == 8:
                    ydata = customerMacCount
                
                if xType == 9:
                    xdata = customerVisitCycle
                if yType == 9:
                    ydata = customerVisitCycle
                
                if xType == 11:
                    xdata = self.__calculation_rate__(newCustomerMacCount,(repeatCustomerMacCount+newCustomerMacCount))
                    
                if yType == 11:
                    ydata = self.__calculation_rate__(newCustomerMacCount,float(repeatCustomerMacCount+newCustomerMacCount))
                data = {'GroupId':int(group),
                        'XData':xdata,
                        'YData':ydata
                        }
                dataList.append(data)
            
            zhimind_result =  {"code":1000,"DataList":dataList}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
            return ws_result
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result
        
        
    def customer_data_keypoint(self,startTime=None,endTime=None,groupList=[],groupType=4,timeType='day'):
        '''39.    顾客类型：关键指标'''
        api_name = 'statis_customer_type/customer_data_keypoint'
        
        if self.runType in [0,1]:
            datas = []
            repeatCustomerMacCountDatas  = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( distinct=True,role=10, groups=groupList, groupType=groupType, st=startTime, et=endTime,factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type,macType=self.macType)
            newCustomerMacCountDatas = myZhimindDatas.get_customer_cnt_from_mac_customer_stats_by_group( distinct=True,role=3, groups=groupList, groupType=groupType, st=startTime, et=endTime,factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type,macType=self.macType)
            avgRepeatCustomerDurDatas = myZhimindDatas.get_customer_dur_from_mac_customer_stats_by_group(role=10,groups=groupList, groupType=groupType, st=startTime, et=endTime, factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type, atype='avg',macType=self.macType)
            avgNewCustomerDurDatas = myZhimindDatas.get_customer_dur_from_mac_customer_stats_by_group(role=3,groups=groupList, groupType=groupType, st=startTime, et=endTime, factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type, atype='avg',macType=self.macType)
            customerVisitCycleDatas = myZhimindDatas.get_avg_visit_cycle_from_customer_type_stats( custType=0,timeType=timeType, groups=groupList, groupType=groupType, day=startTime)
            activeCustomerDatas = {}
            for cust_type in [11,12,13,14]:
                activeCustomerCountDatas = myZhimindDatas.get_active_customer_cnt_from_customer_type_stats_by_group( groups=groupList, groupType=groupType, role=cust_type, st=startTime, et=endTime)
                #activeCustomerDurDatas = myZhimindDatas.get_customer_dur_from_mac_customer_stats_by_group(groups=groupList, groupType=groupType, st=startTime, et=endTime, role=cust_type, factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type, atype='avg')
                activeCustomerDurDatas = myZhimindDatas.get_same_target_from_customer_type_stats(groups=groupList, groupType=groupType, timeType=timeType,  st=startTime, et=endTime, role=cust_type, targetList=['avg_dur'])
                activeCustomerVisitCycleDatas = myZhimindDatas.get_avg_visit_cycle_from_customer_type_stats( custType=cust_type,timeType=timeType, groups=groupList, groupType=groupType, day=startTime)
                activeCustomerVisitCycleDatas = myZhimindDatas.get_visit_cycle_from_mac_customer_stats()
                
                activeCustomerDatas[cust_type] = {'activeCustomerCountDatas':activeCustomerCountDatas,
                                           'activeCustomerDurDatas':activeCustomerDurDatas,
                                           'activeCustomerVisitCycleDatas':activeCustomerVisitCycleDatas
                                           }
    
    
    
            for group in groupList:
                repeatCustomerMacCount  = self.get_value_from_dict_by_key(repeatCustomerMacCountDatas, int(group))
                newCustomerMacCount = self.get_value_from_dict_by_key(newCustomerMacCountDatas, int(group))
                
                repeatCustomerRate = self.__calculation_rate__(repeatCustomerMacCount,(repeatCustomerMacCount+newCustomerMacCount))
                newCustomerRate = self.__calculation_rate__(newCustomerMacCount,(repeatCustomerMacCount+newCustomerMacCount))
                
                
                avgRepeatCustomerDur = self.get_value_from_dict_by_key(avgRepeatCustomerDurDatas, int(group))
                avgNewCustomerDur = self.get_value_from_dict_by_key(avgNewCustomerDurDatas, int(group))
                customerVisitCycle = self.get_value_from_dict_by_key(customerVisitCycleDatas, int(group))
    
                
                LivenessData = []
                
                
                for cust_type in [11,12,13,14]:
                    activeCustomerCount = self.get_value_from_dict_by_key(activeCustomerDatas[cust_type]['activeCustomerCountDatas'], int(group))
                    activeCustomerRate = self.__calculation_rate__(activeCustomerCount,(repeatCustomerMacCount))
                    activeCustomerDur = self.get_value_from_dict_by_key(activeCustomerDatas[cust_type]['activeCustomerDurDatas'], int(group),'avg_dur')
                    activeCustomerVisitCycle = self.get_value_from_dict_by_key(activeCustomerDatas[cust_type]['activeCustomerVisitCycleDatas'], int(group))
                    LivenessData.append({
                                        "Type": cust_type,
                                        "Proportion": activeCustomerRate,
                                        "DurationTime": activeCustomerDur/1000,
                                        "VisitCycle": activeCustomerVisitCycle,
                                        "Count":activeCustomerCount
                                                        })
                
                data={"Group":int(group),
                      "Data":{
                              "New":{
                                     "Count":newCustomerMacCount,
                                     "Proportion":newCustomerRate,
                                     "DurationTime":avgNewCustomerDur/1000,
                                     "VisitCycle":0
                                     },
                              "Old":{
                                     "Count":repeatCustomerMacCount,
                                      "Proportion": repeatCustomerRate,
                                      "DurationTime": avgRepeatCustomerDur/1000,
                                      "VisitCycle": customerVisitCycle,
                                      "LivenessData": LivenessData
                                     }
                              }
                      }
                datas.append(data)
            zhimind_result =  {"code":1000,"DataList":datas}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
            logging.debug(ws_result)
            return ws_result
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result
        
    def customer_data_trend(self,startTime=None,endTime=None,groupList=[],groupType=4,dimension=1,customerType=0):
        '''40.    顾客类型：趋势指标'''
        api_name = 'statis_customer_type/customer_data_trend'
        if dimension == 1:
            timeType = 'day'
        if dimension == 2:
            timeType = 'week'
        if dimension == 3:
            timeType = 'month'
        if self.runType in [0,1]:
            days = myTimeMethod.get_slist_between_st_et(st=startTime, et=endTime, timeType=dimension)
            datas = []
            
            for day in days:
                repeatCustomerMacCountDatas  = myZhimindDatas.get_active_customer_cnt_from_customer_type_stats_by_group( groups=groupList, groupType=groupType, role=10, st=day, et=day,timeType = timeType)
                newCustomerMacCountDatas = myZhimindDatas.get_active_customer_cnt_from_customer_type_stats_by_group( groups=groupList, groupType=groupType, role=3, st=day, et=day,timeType = timeType)
                
                customerTypeStatsDatas = myZhimindDatas.get_same_target_from_customer_type_stats(groups=groupList, groupType=groupType, timeType=dimension, st=day,et=day, role=customerType, targetList=['avg_dur'])
                
                if customerType in [11,12,13,14]:
                    activeCustomerCountDatas = myZhimindDatas.get_active_customer_cnt_from_customer_type_stats_by_group( timeType=timeType,groups=groupList, groupType=groupType, role=customerType, st=day, et=day)
                    
                groupData = []
                for group in groupList:
                    onlineDays = myZhimindDatas.get_shop_open_days(role=customerType,timeType=timeType, group=group, groupType=groupType, st=day, et=day)
                    if  onlineDays ==0:onlineDays=1
                    newCustomerMacCount = self.get_value_from_dict_by_key(newCustomerMacCountDatas, int(group))
                    repeatCustomerMacCount = self.get_value_from_dict_by_key(repeatCustomerMacCountDatas, int(group))
                    if customerType == 3:
                        customerMacCount = newCustomerMacCount
                    elif customerType == 10:
                        customerMacCount = repeatCustomerMacCount
                    else:
                        customerMacCount = self.get_value_from_dict_by_key(activeCustomerCountDatas, int(group))
                        
                    customerRate = self.__calculation_rate__(customerMacCount,(newCustomerMacCount+repeatCustomerMacCount))
                    avgCustomerDur = self.get_value_from_dict_by_key(customerTypeStatsDatas, int(group),sub_key='avg_dur')
                    

                    data = {'Count':round(float(customerMacCount)/onlineDays,self.amount_digit),
                                    
                            'Time':day,
                            'Rate':customerRate
                            }
                    groupData.append(data)
                datas.append({'Group':group,
                              'Data':groupData
                              })
            
            zhimind_result =  {"code":1000,"DataList":datas}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result 
    
    def statis_duration_keypoint(self,startTime=None,endTime=None,groupList=[],groupType=4,customerType=0,timeType=1):
        '''41.    驻店时长：关键指标'''
        api_name = 'statis_duration/statis_duration_keypoint'
        if customerType == 0:
            factory = None
            dis_factory = None
            mac_type = None
            macType = None
        else:
            factory = self.factory
            dis_factory  = self.dis_factory
            mac_type = self.mac_type
            macType = self.macType
        
        if self.runType in [0,1]:
            customerDurData = myZhimindDatas.get_customer_dur_from_mac_customer_stats_by_group( role=customerType, groups=groupList, groupType=groupType, st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type, atype='avg',macType=macType)
            
            deepCustomerCountDatas,jumpCustomerCountDatas = myZhimindDatas.get_deep_jump_customer_cnt_from_mac_customer_stats_by_group( groups=groupList, groupType=groupType, role=customerType, st=startTime, et=endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type,macType=macType)
            
            deepCustomerDatas =  myZhimindDatas.get_same_target_from_customer_type_stats(groups=groupList, groupType=groupType, timeType=timeType, st=startTime,et=endTime, role=customerType, targetList=['deep_visit_sum_dur','cust_visit_cnt'])
            
            last_startTime , last_endTime = myTimeMethod.get_last_st_et(startTime=startTime, endTime=endTime)

            lastCustomerDurData = myZhimindDatas.get_customer_dur_from_mac_customer_stats_by_group( role=0, groups=groupList, groupType=groupType, st=last_startTime, et=last_endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type, atype='avg',macType=macType)
            lastDeepCustomerCountDatas,lastJumpCustomerCountDatas = myZhimindDatas.get_deep_jump_customer_cnt_from_mac_customer_stats_by_group( groups=groupList, groupType=groupType, role=customerType, st=last_startTime, et=last_endTime, factory=factory, dis_factory=dis_factory, mac_type=mac_type,macType=macType)
            
            lastDeepCustomerDatas =  myZhimindDatas.get_same_target_from_customer_type_stats(groups=groupList, groupType=groupType, timeType=timeType, st=last_startTime,et=last_endTime, role=customerType, targetList=['deep_visit_sum_dur','cust_visit_cnt'])
            
            datas = []
            for group in groupList:
                group = int(group)
                customerDur = self.get_value_from_dict_by_key(customerDurData, group)
                customerCount = self.get_value_from_dict_by_key(deepCustomerDatas, group,'cust_visit_cnt')
                depCustomerSumDur = self.get_value_from_dict_by_key(deepCustomerDatas, group,'deep_visit_sum_dur')
                deepCustomerCount = self.get_value_from_dict_by_key(deepCustomerCountDatas, group)

                if deepCustomerCount == 0:
                    deepCustomerDur = 0
                else:
                    deepCustomerDur = self.__calculation_avg__(depCustomerSumDur,deepCustomerCount)
                
                lastCustomerCount = self.get_value_from_dict_by_key(lastDeepCustomerDatas, group,'cust_visit_cnt')
                lastDeepCustomerCount = self.get_value_from_dict_by_key(lastDeepCustomerCountDatas, group)
               
                deepCustomerCountChange = self.__calculation_mom__(deepCustomerCount, lastDeepCustomerCount)
                
                
                deepCustomerRate = self.__calculation_rate__(deepCustomerCount,customerCount)
                
                lastDeepCustomerRate = self.__calculation_rate__(lastDeepCustomerCount,lastCustomerCount)
                if lastDeepCustomerRate == 0:
                    deepCustomerRateChange = 0
                else:
                    deepCustomerRateChange = self.__calculation_mom__(deepCustomerRate, lastDeepCustomerRate)
                
                
                jumpCustomerCount = self.get_value_from_dict_by_key(jumpCustomerCountDatas, group)
                lastJumpCustomerCount = self.get_value_from_dict_by_key(lastJumpCustomerCountDatas, group)
                
                jumpCustomerCountChange = self.__calculation_mom__(jumpCustomerCount, lastJumpCustomerCount)
                
                
                jumpCustomerRate = self.__calculation_rate__(jumpCustomerCount,customerCount)
                lastJumpCustomerRate = self.__calculation_rate__(lastJumpCustomerCount,lastCustomerCount)
                if lastJumpCustomerRate == 0:
                    jumpCustomerRateChange = 0
                else:
                    jumpCustomerRateChange =  self.__calculation_mom__(jumpCustomerRate , lastJumpCustomerRate) 
                
                data = {      "Group": group,
                            "DeepCustomerCount":deepCustomerCount,
                            "DeepCustomerCountChange":deepCustomerCountChange,
                            "DurationTime":customerDur/1000 ,
                            "DeepCustomerDurationTime": int(deepCustomerDur)/1000,
                            "DeepCustomerRate": deepCustomerRate,
                            "DeepCustomerRateChange": deepCustomerRateChange,
                            "OutCustomerCount":jumpCustomerCount,
                            "OutCustomerRate": jumpCustomerRate,
                            "OutCustomerRateChange": jumpCustomerRateChange,
                            "OutCustomerCountChange":jumpCustomerCountChange
                        }
                datas.append(data)
            zhimind_result =  {"code":1000,"DataList":datas}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result
    
    
    def __calculation_mom__(self,num1,num2):
        if num2==0:
            return 0
        if num1 == 0 : return -1
        return round(float(num1)/num2 -1,self.rate_digit)
    
    
    def __calculation_rate__(self,num1,num2):
        if num2==0:
            return 0
        return round(float(num1)/num2,self.rate_digit)
    
    def __calculation_avg__(self,num1,count,amount_digit=0):
        if count==0:
            return 0
        return round(float(num1)/count,amount_digit)
    
            
    def customer_quality(self,timeType=1,startTime=None,groupList=[],groupType=4):
        '''48.    店铺概览：客流质量'''
        api_name = 'shop_summary/customer_quality'
        
        if self.runType in [0,1]:
        
            customerVisitCycleDatas = myZhimindDatas.get_avg_visit_cycle_from_customer_type_stats( timeType=timeType, groups=groupList, groupType=groupType, day=startTime, custType=0)
            repeatCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=10, groups=groupList, groupType=groupType, st=startTime, et=startTime)
            newCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=3, groups=groupList, groupType=groupType, st=startTime, et=startTime)
            highCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=11, groups=groupList, groupType=groupType, st=startTime, et=startTime)
            middleCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=12, groups=groupList, groupType=groupType, st=startTime, et=startTime)
            lowCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=13, groups=groupList, groupType=groupType, st=startTime, et=startTime)
            sleepCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=14, groups=groupList, groupType=groupType, st=startTime, et=startTime)
            
            
            last_startTime , last_endTime = myTimeMethod.get_last_st_et_by_timeType(startTime=startTime, timeType=timeType)
            
            lastCustomerVisitCycleDatas = myZhimindDatas.get_avg_visit_cycle_from_customer_type_stats( timeType=timeType, groups=groupList, groupType=groupType, day=last_startTime, custType=0)
            lastRepeatCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=10, groups=groupList, groupType=groupType, st=last_startTime, et=last_startTime)
            lastNewCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=3, groups=groupList, groupType=groupType, st=last_startTime, et=last_startTime)
            lastHighCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=11, groups=groupList, groupType=groupType, st=last_startTime, et=last_startTime)
            lastMiddleCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=12, groups=groupList, groupType=groupType, st=last_startTime, et=last_startTime)
            lastLowCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=13, groups=groupList, groupType=groupType, st=last_startTime, et=last_startTime)
            lastSleepCustomerMacCountDatas = myZhimindDatas.get_customer_dist_cnt_from_customer_type_stats_by_group( timeType=timeType, role=14, groups=groupList, groupType=groupType, st=last_startTime, et=last_startTime)
            datas = []
            for group in groupList:
                group = int(group)
                
                customerVisitCycle = self.get_value_from_dict_by_key(customerVisitCycleDatas, group)
                lastCustomerVisitCycle = self.get_value_from_dict_by_key(lastCustomerVisitCycleDatas, group)
                
                customerVisitCycleChange = self.__calculation_mom__(customerVisitCycle, lastCustomerVisitCycle)
                
                repeatCustomerMacCount = self.get_value_from_dict_by_key(repeatCustomerMacCountDatas, group)
                lastRepeatCustomerMacCount = self.get_value_from_dict_by_key(lastRepeatCustomerMacCountDatas, group)
    
                newCustomerMacCount = self.get_value_from_dict_by_key(newCustomerMacCountDatas, group)
                lastNewCustomerMacCount = self.get_value_from_dict_by_key(lastNewCustomerMacCountDatas, group)
                
                allCustomerMacCount = repeatCustomerMacCount + newCustomerMacCount
                lastAllCustomerMacCount = lastRepeatCustomerMacCount + lastNewCustomerMacCount
                
                
                newCustomerRate = self.__calculation_rate__(newCustomerMacCount, allCustomerMacCount)
                
                lastNewCustomerRate = self.__calculation_rate__(lastNewCustomerMacCount,lastAllCustomerMacCount)
                
                newCustomerRateChange = self.__calculation_mom__(newCustomerRate, lastNewCustomerRate)
                
                repeatCustomerRate = self.__calculation_rate__(repeatCustomerMacCount,allCustomerMacCount)
                lastRepeatCustomerRate = self.__calculation_rate__(lastRepeatCustomerMacCount,lastAllCustomerMacCount)
                
                repeatCustomerRateChange = self.__calculation_mom__(repeatCustomerRate,lastRepeatCustomerRate)
                
                
                highCustomerMacCount =self.get_value_from_dict_by_key(highCustomerMacCountDatas, group)
                lastHighCustomerMacCount =self.get_value_from_dict_by_key(lastHighCustomerMacCountDatas, group)
                
                highCustomerMacCountRate = self.__calculation_rate__(highCustomerMacCount,repeatCustomerMacCount)
                lastHighCustomerMacCountRate = self.__calculation_rate__(lastHighCustomerMacCount,lastRepeatCustomerMacCount)
                highCustomerMacCountRateChange = self.__calculation_mom__(highCustomerMacCountRate,lastHighCustomerMacCountRate)
                
                
                middleCustomerMacCount = self.get_value_from_dict_by_key(middleCustomerMacCountDatas, group)
                lastMiddleCustomerMacCount = self.get_value_from_dict_by_key(lastMiddleCustomerMacCountDatas, group)
                
                middleCustomerMacCountRate = self.__calculation_rate__(middleCustomerMacCount,repeatCustomerMacCount)
                lastMiddleCustomerMacCountRate = self.__calculation_rate__(lastMiddleCustomerMacCount,lastRepeatCustomerMacCount)
                middleCustomerMacCountRateChange = self.__calculation_mom__(middleCustomerMacCountRate,lastMiddleCustomerMacCountRate)
                
                
                lowCustomerMacCount = self.get_value_from_dict_by_key(lowCustomerMacCountDatas, group)
                lastLowCustomerMacCount = self.get_value_from_dict_by_key(lastLowCustomerMacCountDatas, group)
                
                lowCustomerMacCountRate = self.__calculation_rate__(lowCustomerMacCount,repeatCustomerMacCount)
                lastLowCustomerMacCountRate = self.__calculation_rate__(lastLowCustomerMacCount,lastRepeatCustomerMacCount)
                lowCustomerMacCountRateChange = self.__calculation_mom__(lowCustomerMacCountRate,lastLowCustomerMacCountRate)
                
                
                
                sleepCustomerMacCount = self.get_value_from_dict_by_key(sleepCustomerMacCountDatas, group)
                lastSleepCustomerMacCount = self.get_value_from_dict_by_key(lastSleepCustomerMacCountDatas, group)
                
                sleepCustomerMacCountRate = self.__calculation_rate__(sleepCustomerMacCount,repeatCustomerMacCount)
                lastSleepCustomerMacCountRate = self.__calculation_rate__(lastSleepCustomerMacCount,lastRepeatCustomerMacCount)
                sleepCustomerMacCountRateChange = self.__calculation_mom__(sleepCustomerMacCountRate,lastSleepCustomerMacCountRate)
                
                
                data = {'Group':group,
                        'Data':{
                                "VisitCycle":{
                                              "Count":customerVisitCycle,
                                              "Change":customerVisitCycleChange,
                                              },
                                "New":{
                                       "Count":newCustomerMacCount,
                                       "Proportion":newCustomerRate,
                                       "Change":newCustomerRateChange
                                       },
                                "Old":{
                                       "Count":repeatCustomerMacCount,
                                       "Proportion":repeatCustomerRate,
                                       "Change":repeatCustomerRateChange,
                                       "LivenessData":[
                                                       {"Type":11,
                                                        "Count":highCustomerMacCount,
                                                        "Proportion":highCustomerMacCountRate,
                                                        "Change":highCustomerMacCountRateChange
                                                        },
                                                       {"Type":12,
                                                        "Count":middleCustomerMacCount,
                                                        "Proportion":middleCustomerMacCountRate,
                                                        "Change":middleCustomerMacCountRateChange
                                                        },
                                                       {"Type":13,
                                                        "Count":lowCustomerMacCount,
                                                        "Proportion":lowCustomerMacCountRate,
                                                        "Change":lowCustomerMacCountRateChange
                                                        },
                                                       {"Type":14,
                                                        "Count":sleepCustomerMacCount,
                                                        "Proportion":sleepCustomerMacCountRate,
                                                        "Change":sleepCustomerMacCountRateChange
                                                        },
                                                       ]
                                       }
                                
                                }
                        }
            
                datas.append(data)
            
            zhimind_result =  {"code":1000,"DataList":datas}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result
            
            
    def stats_visit_frequency_scatter_grouplist(self,startTime=None,endTime=None,customerType=0,groupList = [] , groupType = 4):
        '''60.    到访次数分布 - GroupList'''
        api_name = 'statis_frequency/stats_visit_frequency_scatter_grouplist'
        
        if self.runType in [0,1]:
            
            rules = {"1":[1,1],"2":[2,2],"3":[3,5],"4":[5,None]}
            customerVisitFreqScatterDatas = myZhimindDatas.get_customer_visit_freq_scatter(st=startTime, et=endTime, groups=groupList, groupType=groupType, factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type, macType=self.macType,rules=rules)
            datas = []
            for group in groupList:
                group = int(group)
                if group not in customerVisitFreqScatterDatas.keys():
                    for rtype in rules.keys():
                        datas.append({'Group':group,'Count':0,"Type":rtype})

                customerVisitFreqScatterData = customerVisitFreqScatterDatas[group]
                for type_num in rules.keys():
                    if type_num in customerVisitFreqScatterData.keys():
                        type_value = customerVisitFreqScatterData[type_num]
                    else:
                        type_value = 0
                    datas+=[{'Group':group,'Count':type_value,"Type":type_num}]
            
            zhimind_result =  {"code":1000,"DataList":datas}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result
                
    def stats_vf_duration_scatter_grouplist(self,startTime=None,endTime=None,customerType=0,groupList = [] , groupType = 4):
        '''61.    到访次数驻店时长分布 - GroupList'''
        api_name = 'statis_duration/stats_vf_duration_scatter_grouplist'
        if self.runType in [0,1]:
            rules = {1:[1,1],2:[2,2],3:[3,5],4:[5,None]}
            customerDurScatterDatas = myZhimindDatas.get_customer_dur_scatter_with_visit_freq(st=startTime, et=endTime, groups=groupList, groupType=groupType, factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type, macType=self.macType,rules=rules)
            datas = []
            for group in groupList:
                group = int(group)
                if group not in customerDurScatterDatas.keys():
                    datas+=[{'Group':group,'Count':0,"DurationType":1,'FrequencyType':1},
                            {'Group':group,'Count':0,"DurationType":2,'FrequencyType':1},
                            {'Group':group,'Count':0,"DurationType":3,'FrequencyType':1},
                            {'Group':group,'Count':0,"DurationType":4,'FrequencyType':1},
                            {'Group':group,'Count':0,"DurationType":1,'FrequencyType':2},
                            {'Group':group,'Count':0,"DurationType":2,'FrequencyType':2},
                            {'Group':group,'Count':0,"DurationType":3,'FrequencyType':2},
                            {'Group':group,'Count':0,"DurationType":4,'FrequencyType':2},
                            {'Group':group,'Count':0,"DurationType":1,'FrequencyType':3},
                            {'Group':group,'Count':0,"DurationType":2,'FrequencyType':3},
                            {'Group':group,'Count':0,"DurationType":3,'FrequencyType':3},
                            {'Group':group,'Count':0,"DurationType":4,'FrequencyType':3},
                            {'Group':group,'Count':0,"DurationType":1,'FrequencyType':4},
                            {'Group':group,'Count':0,"DurationType":2,'FrequencyType':4},
                            {'Group':group,'Count':0,"DurationType":3,'FrequencyType':4},
                            {'Group':group,'Count':0,"DurationType":4,'FrequencyType':4},
                            ]
                    continue
                
                customerDurScatterData = customerDurScatterDatas[group]
                for freqType , cdata in  customerDurScatterData.items():
                    for durType , count in cdata.items():
                        datas.append({"Group":group,"Count":count,"DurationType":durType,"FrequencyType":freqType})
                
                
            zhimind_result =  {"code":1000,"DataList":datas}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result
    
    def stats_vf_duration_scatter(self,startTime=None,endTime=None,group = None, groupType = 4):
        '''22.    到访次数驻店时长分布'''
        api_name = 'statis_duration/stats_vf_duration_scatter'
        if self.runType in [0,1]:
            rules = {1:[1,1],2:[2,2],3:[3,5],4:[5,None]}
            customerDurScatterDatas = myZhimindDatas.get_customer_dur_scatter_with_visit_freq(st=startTime, et=endTime, groups=[group], groupType=groupType, factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type, macType=self.macType,rules=rules)
            datas = []

            if group not in customerDurScatterDatas.keys():
                datas+=[{'Group':group,'Count':0,"DurationType":1,'FrequencyType':1},
                        {'Group':group,'Count':0,"DurationType":2,'FrequencyType':1},
                        {'Group':group,'Count':0,"DurationType":3,'FrequencyType':1},
                        {'Group':group,'Count':0,"DurationType":4,'FrequencyType':1},
                        {'Group':group,'Count':0,"DurationType":1,'FrequencyType':2},
                        {'Group':group,'Count':0,"DurationType":2,'FrequencyType':2},
                        {'Group':group,'Count':0,"DurationType":3,'FrequencyType':2},
                        {'Group':group,'Count':0,"DurationType":4,'FrequencyType':2},
                        {'Group':group,'Count':0,"DurationType":1,'FrequencyType':3},
                        {'Group':group,'Count':0,"DurationType":2,'FrequencyType':3},
                        {'Group':group,'Count':0,"DurationType":3,'FrequencyType':3},
                        {'Group':group,'Count':0,"DurationType":4,'FrequencyType':3},
                        {'Group':group,'Count':0,"DurationType":1,'FrequencyType':4},
                        {'Group':group,'Count':0,"DurationType":2,'FrequencyType':4},
                        {'Group':group,'Count':0,"DurationType":3,'FrequencyType':4},
                        {'Group':group,'Count':0,"DurationType":4,'FrequencyType':4},
                        ]
            else:
                
                customerDurScatterData = customerDurScatterDatas[group]
                for freqType , cdata in  customerDurScatterData.items():
                    for durType , count in cdata.items():
                        datas.append({"Group":group,"Count":count,"DurationType":durType,"FrequencyType":freqType})
                
                
            zhimind_result =  {"code":1000,"DataList":datas}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result
    
    def stats_visit_frequency_scatter(self,startTime=None,endTime=None,customerType=0,group=None,groupType=4):
        u'''10.    到访次数分布'''
        api_name = 'statis_frequency/stats_visit_frequency_scatter'
        
        if self.runType in [0,1]:
            rules = {"1":[1,1],"2":[2,2],"3":[3,5],"4":[5,None]}
            customerFreqScatterDatas = myZhimindDatas.get_customer_visit_freq_scatter(st=startTime, et=endTime, role=customerType, groups=[group], groupType=groupType, factory=self.factory, dis_factory=self.dis_factory, mac_type=self.mac_type, rules=rules,macType=self.macType)
        
   
            datas = []
            for group in [group]:
                group = int(group)
                if group not in customerFreqScatterDatas.keys():
                    datas.append({"Count":0,"Type":1})
                    datas.append({"Count":0,"Type":2})
                    datas.append({"Count":0,"Type":3})
                    datas.append({"Count":0,"Type":4})
                    
                    continue
                
                customerFreqScatterData = customerFreqScatterDatas[group]
                freqTypeList =customerFreqScatterData.keys()
                freqTypeList.sort()
                for freqType  in  freqTypeList:
                    freq = customerFreqScatterData[freqType]
                    datas.append({"Count":freq,"Type":freqType})
            
            zhimind_result =  {"code":1000,"DataList":datas}
        
        if self.runType in [0,2]:
            params = self.get_params_from_method(inspect.getargvalues(inspect.currentframe()))
            
            ws_result = self.getResponse(url=self.base+'/'+api_name, detailMap=params)
        if self.runType == 0:
            if zhimind_result == ws_result:
                logging.debug(json.dumps(ws_result))
                logging.info("Check OK.")
            else:
                logging.warn(json.dumps(zhimind_result)) 
                logging.warn(json.dumps(ws_result))
                logging.warn("Check Faild.")
        
        return zhimind_result
if __name__=="__main__":
    
    logging.basicConfig(level = logging.DEBUG)
    

    groupList = [32012194]
    groupType = 4
    timeType = 2
    requestType = 4
    xType = 1
    yType = 2
    dimension = 1
    customerType = 0
    if timeType == 1:
        startTime = endTime ="2015-09-25"
    elif timeType == 2:
        startTime = '2015-09-21'
        endTime = '2015-09-27'
    elif timeType == 3:
        startTime = '2015-09-01'
        endTime ='2015-09-30'
    else:
        startTime = '2015-07-01'
        endTime = '2015-09-30'
    
    base='http://192.168.1.208:18080/zm-ws-3.0/api/v3'
    myVerifyZhimindWSData = VerifyZhimindWSData(base=base,runType=0)
    # print myVerifyZhimindWSData.customer_data_keypoint(startTime, endTime, groupList, groupType)
    # print myVerifyZhimindWSData.data_cross_bubble(requestType=requestType, timeType=1, startTime=startTime, groupList=groupList, groupType=groupType, xType=xType, yType=yType)       
    # print myVerifyZhimindWSData.customer_data_trend(startTime=startTime, endTime=endTime, groupList=groupList, dimension=dimension, customerType=customerType)
    #  
    # print myVerifyZhimindWSData.statis_duration_keypoint(startTime=startTime, endTime=endTime, groupList=groupList, groupType=groupType, customerType=customerType)
    #print myVerifyZhimindWSData.customer_quality(timeType, startTime, groupList)
    # print myVerifyZhimindWSData.stats_vf_duration_scatter_grouplist(startTime=startTime,endTime=endTime,customerType=customerType,groupList =groupList , groupType = groupType)
    
    #print myVerifyZhimindWSData.stats_visit_frequency_scatter(startTime, endTime, customerType, group=groupList[0], groupType=groupType)
    # print myVerifyZhimindWSData.stats_visit_frequency_scatter_grouplist(startTime, endTime, customerType, groupList, groupType)

